package com.itheima.user.controller;

import com.itheima.common.es.domain.Goods;
import com.itheima.common.response.ResponseResult;
import com.itheima.user.ro.LoginRO;
import com.itheima.user.service.UserService;
import com.itheima.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author liqp
 * @version 1.0
 * @date 2021/1/28
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 登陆
     *
     * @param loginRO
     * @return
     */
    @PostMapping("login")
    public ResponseResult<String> login(@RequestBody LoginRO loginRO) {
        String login = userService.login(loginRO);
        return new ResponseResult<>(login);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseResult<Void> delete(@PathVariable("id") Integer id) {
        userService.delete(id);
        return new ResponseResult<Void>();
    }

    @GetMapping("{id}")
    public ResponseResult<UserVO> get(@PathVariable("id") Integer id) throws Exception {
        UserVO userVO = userService.get(id);
        return new ResponseResult<>(userVO);
    }

    /**
     * 通过feign调用es的服务搜索商品
     *
     * @return
     * @throws IOException
     */
    @GetMapping("search/goods")
    public ResponseResult<List<Goods>> search() throws IOException {
        return userService.serchGoods();
    }

}
